/**********************************************
** @Author: phy
** @Date:   2017-12-12 14:17:37
** @Last Modified by:   phy
***********************************************/
package controllers

import (
	//"github.com/astaxie/beego"
	"fmt"
	//"strconv"
	"strings"
	//"time"

	//"github.com/astaxie/beego"
	//"WebAdminSys/libs"
	"WebAdminSys/models"
)

type SysGamenoticeController struct {
	BaseController
}

func (self *SysGamenoticeController) List() {
	fmt.Println("==================SysGamenoticeController List========================");
	self.Data["pageTitle"] = "游戏公告"
	//self.Ctx.WriteString("MangeAdminController.List")
	self.display()
}

func (self *SysGamenoticeController) Add() {
	if  ! self.isPost(){
		fmt.Println("================== GET SysGamenoticeController Add =======================");
		self.Data["pageTitle"] = "添加公告"
	
		self.display()
		fmt.Println(self.TplName)

	}else
	{
		//beego.ReadFromRequest(&self.Controller)
		content := strings.TrimSpace(self.GetString("content"));
		timeinterval,_:= self.GetInt64("timeInterval");
		repeat,_:= self.GetInt64("repeat");
	
		fmt.Println("================== POST SysGamenoticeController Add =======================");
		fmt.Println(content,timeinterval,repeat);

		nl:= models.NoticeCtroInfoGetInsertValue();
		nl.CONTENT = content;
		nl.SEPTIME =timeinterval; 
		nl.TIMES =repeat;
		n,err := models.NoticeCtroInfoInsertValue(nl);
		if err != nil {
			self.ajaxMsg(err.Error(),MSG_ERR)
			fmt.Println("n = ",n,"err=", err.Error())
		}else{
			self.ajaxMsg(n,MSG_OK)
			fmt.Println("n = ",n)
		}
		
	}
	
	
	
}


func (self *SysGamenoticeController) AjaxSave() {
	
	
}

func (self *SysGamenoticeController) AjaxDel() {
	fmt.Println("==================SysGamenoticeController AjaxDel========================");
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 30
	}
	self.pageSize = limit

	id,_ := self.GetInt("id")
	filters := make([]interface{}, 0)
	filters = append(filters, "id",id)
	fmt.Println("page:",page,"limit:",limit,"id:",id)
	num,err := models.NoticeCtroInfoDelete(page,limit,filters...);


	if  err != nil {
		self.ajaxMsg(err.Error(), MSG_ERR)
	}
	self.ajaxMsg(num, MSG_OK)
}

func (self *SysGamenoticeController) Table() {
	fmt.Println("==================SysGamenoticeController Table========================");
	//列表
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 30
	}


	noticeContent := self.GetString("noticeContent")

	fmt.Println("noticeContent:",noticeContent,"page",page,"limit",limit)

	self.pageSize = limit
	//查询条件
	filters := make([]interface{}, 0)
	if noticeContent != ""{
		filters = append(filters, "CONTENT__icontains", noticeContent)
	}
	
	
	result, count := models.NoticeCtroInfoGetList(page, self.pageSize, filters...)
	list := make([]map[string]interface{}, len(result))
	for k, v := range result {
		row := make(map[string]interface{})
		row["noticeContent"] = v.CONTENT
		row["playInterval"] = v.SEPTIME
		row["repeatCount"] = v.TIMES
		row["operation"] = v.STATUS
		row["id"] = v.ID
	
		list[k] = row
	}
	self.ajaxList("成功", MSG_OK, count, list)
}

